//
//  Offer_12.swift
//  LeetCode
//
//  Created by 刘彬彬 on 2021/1/21.
//  Copyright © 2021 LBB. All rights reserved.
//  请设计一个函数，用来判断在一个矩阵中是否存在一条包含某字符串所有字符的路径。路径可以从矩阵中的任意一格开始，每一步可以在矩阵中向左、右、上、下移动一格。如果一条路径经过了矩阵的某一格，那么该路径不能再次进入该格子。例如，在下面的3×4的矩阵中包含一条字符串“bfce”的路径（路径中的字母用加粗标出）。

//[["a","b","c","e"],
//["s","f","c","s"],
//["a","d","e","e"]]
//
//但矩阵中不包含字符串“abfb”的路径，因为字符串的第一个字符b占据了矩阵中的第一行第二个格子之后，路径不能再次进入这个格子。

//输入：board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCCED"
//输出：true

//输入：board = [["a","b"],["c","d"]], word = "abcd"
//输出：false


import Foundation
public class MatrixLoction {
    public var hang : Int = 0
    public var lie : Int = 0
}

class Offer_12 {
    func exist(_ board: [[Character]], _ word: String) -> Bool {
        let charArray: [Character] = Array(word)
        var matrixLoction : [[Int]] = []
        
        if board.count == 1 {
            let temp:[Character] = board[0]
            if temp.count == 1 && word.count == 1 {
                let one = temp[0]
                if String(one) != word {
                    return false
                }
            }
        }
        
        for i in 0..<charArray.count {
            let zimu = charArray[i]
            //获取当前字母在矩阵中的位置
            let currentvalueAllLoction : [[Int]] = getLoction(board, zimu)
            if i != 0 {
                //看当前字母是否在上个字母的路径当中
                var existPath :Bool = false
                for j in 0..<currentvalueAllLoction.count {
                    if matrixLoction.contains(currentvalueAllLoction[j]) {
                        existPath = true
                    }
                }
                //如果没找到路径，直接返回false
                if !existPath {
                    return false
                }
                matrixLoction = getEdgesLoction(board, zimu,i)
            } else {
                matrixLoction = getEdgesLoction(board, zimu,i)
            }
        }
        
        return true
    }
    
    //获取当前元素在矩阵中的所有位置
    func getLoction(_ board: [[Character]], _ word: Character) -> [[Int]] {
        var matrixLoction : [[Int]] = []
        for i in 0..<board.count {
            let temp:[Character] = board[i]
            if temp.contains(word) {
                for (index,value) in temp.enumerated() {
                    if value == word {
                        matrixLoction.append([i,index])
                    }
                }
            }
        }
        return matrixLoction
    }
    
    //获取位置i元素在矩阵中，上下左右位置的坐标
    func getEdgesLoction(_ board: [[Character]], _ word: Character, _ i:Int) -> [[Int]] {
        var matrixLoction : [[Int]] = []
        for i in 0..<board.count {
            let temp:[Character] = board[i]
            if temp.contains(word) {
                for (index,value) in temp.enumerated() {
                    if value == word {
                        //添加左
                        if index > 0 {
                            //左边点在矩阵中的坐标
                            matrixLoction.append([i,index - 1])
                        }
                        //添加上
                        if i > 0 {
                            matrixLoction.append([i-1,index])
                        }
                        //添加右
                        if index < (temp.count - 1) {
                            matrixLoction.append([i,index + 1])
                        }
                        //添加下
                        if i < (board.count - 1) {
                            matrixLoction.append([i + 1,index])
                        }
                    }
                }
            }
        }
        return matrixLoction
    }
}
